package org.ctd.springframework.beans.factory.support;

import com.ctd.springframework.core.io.ResourceLoader;
import com.ctd.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.ctd.springframework.util.Assert;

/**
 * AbstractBeanDefinitionReader
 *
 * @author chentudong
 * @time 2019/3/24 20:39
 */
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader {
    private BeanDefinitionRegistry registry;
    private ResourceLoader resourceLoader;
    private ClassLoader beanClassLoader;

    protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {
        Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
        this.registry = registry;
        if (this.registry instanceof ResourceLoader) {
            this.resourceLoader = (ResourceLoader) this.registry;
        } else {
            //创建 ResourceLoader
            this.resourceLoader = new PathMatchingResourcePatternResolver();
        }
    }

    @Override
    public ResourceLoader getResourceLoader() {
        return this.resourceLoader;
    }

    public void setBeanClassLoader(ClassLoader beanClassLoader) {
        this.beanClassLoader = beanClassLoader;
    }

    /**
     * getBeanClassLoader
     *
     * @return ClassLoader
     */
    @Override
    public ClassLoader getBeanClassLoader() {
        return this.beanClassLoader;
    }
}
